package com.wteam.dragon.system.apps.server.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wteam.dragon.system.apps.server.povo.bean.Server;
import com.wteam.dragon.system.apps.server.mapper.ServerMapper;
import com.wteam.dragon.system.apps.server.service.ServerService;
import com.wteam.dragon.system.apps.server.util.ExecuteShellUtil;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.exception.BadRequestException;
import com.wteam.dragon.system.util.ValidationUtil;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2020/3/23
 */
@Service
@RequiredArgsConstructor
public class ServerServiceImpl implements ServerService {
    private final ServerMapper serverMapper;

    /**
     * shell ls 命令
     */
    private final static String LS = "ls";

    @Override
    public ResultMessage testConnect(Server server) {
        ResultMessage resultMessage = new ResultMessage();
        ExecuteShellUtil executeShellUtil= null;
        try {
            executeShellUtil = new ExecuteShellUtil(server.getIp(), server.getAccount(), server.getPassword(),server.getPort());
            if((executeShellUtil.execute(LS) != 0)){
                throw new BadRequestException("连接失败");
            }
            resultMessage.setStatus(HttpStatus.OK.value());
            resultMessage.setMessage("连接成功");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            Dict resultMap = Dict.create().set("reason", e.getMessage());
            resultMessage.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            resultMessage.setMessage("请求失败，请沟通后台");
            resultMessage.setResultParam(resultMap);
        }finally {
            if (executeShellUtil != null) {
                executeShellUtil.close();
            }
        }
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addServer(Server server) {
        serverMapper.insert(server);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delServer(Long id) {
        serverMapper.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editServer(Server server) {
        serverMapper.updateById(server);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更新成功");
        return resultMessage;
    }

    @Override
    public ResultMessage listServer() {
        QueryWrapper<Server> serverQueryWrapper = new QueryWrapper<>();
        List<Server> serverList = serverMapper.selectList(serverQueryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("serverList", serverList);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }
}
